//
//  NetworkTools.swift
//  06-AFNetworking的封装
//
//  Created by xiaomage on 16/4/6.
//  Copyright © 2016年 小码哥. All rights reserved.
//

import AFNetworking

// 定义枚举类型
enum RequestType : String {
    case GET = "GET"
    case POST = "POST"
}

class NetworkTools: AFHTTPSessionManager {
    // let是线程安全的
    static let shareInstance : NetworkTools = {
        let tools = NetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
}

// MARK:- 封装请求方法
extension NetworkTools {
    func request(_ methodType : RequestType, urlString : String, parameters : [String : AnyObject], finished : @escaping (_ result : AnyObject?, _ error : NSError?) -> ()) {
        
        // 1.定义成功的回调闭包
        let successCallBack = { (task : URLSessionDataTask, result : Any?) -> Void in
            finished(result as AnyObject?, nil)
        }
        
        // 2.定义失败的回调闭包
        let failureCallBack = { (task : URLSessionDataTask?, error : Error) -> Void in
            finished(nil, error as NSError?)
        }
        
        // 3.发送网络请求
        if methodType == .GET {
            get(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallBack)
        } else {
            post(urlString, parameters: parameters, progress: nil, success: successCallBack, failure: failureCallBack)
        }
        
    }
}

// MARK:- 请求AccessToken
extension NetworkTools {
    func loadAccessToken(code : String, finished : @escaping (_ result : [String : AnyObject]?, _ error : NSError?) -> ()) {
        // 1.获取请求的URLString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        // 2.获取请求的参数
        let parameters = ["client_id" : appKey, "client_secret" : appSecret, "grant_type" : "authorization_code", "redirect_uri" : redirect_uri, "code" : code]
        
        // 3.发送网络请求
        request(.POST, urlString: urlString, parameters: parameters as [String : AnyObject]) { (result, error) -> () in
            finished(result as? [String : AnyObject], error)
        }
    }
}

// MARK:- 请求用户信息
extension NetworkTools{
    func loadUserInfo(accessToken : String, uid : String,  finished : @escaping (_ result : [String : AnyObject]?, _ error : NSError?) -> ()) {
        //地址
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        //参数
        let para = ["access_token" : accessToken, "uid" : uid]
        
        //请求
        request(.GET, urlString: urlString, parameters: para as [String : AnyObject]) { (result, error) ->() in
            
            finished(result as! [String : AnyObject]?, error)
            
        }
        
        
        
    }

}

// MARK:- 获取首页微博数据
extension NetworkTools{

  //  func loadStatuses(finished : @escaping (_ result : [[String : AnyObject]]?, _ error: NSError) -> ()) {
        
//        let statusURL = "https://api.weibo.com/2/statuses/home_timeline.json"
//        let para = ["access_token" : (UserAccountViewModel.shareIntance.account?.access_token)!]
//        
//        // 请求
//        request(.GET, urlString: statusURL, parameters: para as [String : AnyObject]) { (result, error) in
//            
//            //取出微博数据
//            guard let resultDic = result as? [String : AnyObject] else{
//                
//                finished(nil, error!)
//                return
//            }
//            
//            //返回微博数据
//            finished(resultDic["statuses"] as! [[String : AnyObject]]?, error!)
//            
//        }
//    
//    }


func loadStatuses(finished : @escaping (_ result : [[String : AnyObject]]?, _ error : NSError?) -> ()) {
    // 1.获取请求的URLString
    let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
    
    // 2.获取请求的参数
    let parameters = ["access_token" : (UserAccountViewModel.shareIntance.account?.access_token)!]
    
    // 3.发送网络请求
    request(.GET, urlString: urlString, parameters: parameters as [String : AnyObject]) { (result, error) -> () in
        
        // 1.获取字典的数据
        guard let resultDict = result as? [String : AnyObject] else {
            finished(nil, error)
            return
        }
        
        // 2.将数组数据回调给外界控制器
        finished(resultDict["statuses"] as? [[String : AnyObject]], error)
    }
}


}





